package com.foo.servlets;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.example.www.FacadeInterfaceServiceStub.EventsFacade;
import org.example.www.FacadeInterfaceServiceStub.EventsFacadeList;
import org.example.www.FacadeInterfaceServiceStub.GetEventsByDateFault;
import org.example.www.FacadeInterfaceServiceStub.GetEventsByKeywordFault;
import org.example.www.FacadeWSClient;
import org.example.www.GetEventsByDateFaultException;
import org.example.www.GetEventsByKeywordFaultException;

/**
 * Servlet implementation class EventServlet
 */
public class EventServlett extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public EventServlett() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		RequestDispatcher dispatcher;
		HttpSession session = request.getSession(true);
		EventsFacadeList eventsArray = null;
		
		session.setAttribute("EventsArrayData", null);
		FacadeWSClient client = new FacadeWSClient();
		
		String sortBased = request.getParameter("SortBased");
		
		if(sortBased.equalsIgnoreCase("eventsbykeyword"))
		{
			String keyword = request.getParameter("eventskeyword");
			if(keyword.equalsIgnoreCase(""))
			{
				request.setAttribute("ServerMessage","Please Enter value in required* fields!");
				dispatcher = getServletContext().getRequestDispatcher("/events.jsp");
				dispatcher.forward(request, response);
			}
			try {
				eventsArray = client.getEventsByKeyword(keyword);
			} catch(GetEventsByKeywordFaultException e) 
			{
				GetEventsByKeywordFault fault = e.getFaultMessage();
				String exceptionString = new String();
				exceptionString= fault.getGetEventsByKeywordFault();
				
				request.setAttribute("ServerMessage",exceptionString);
				dispatcher = getServletContext().getRequestDispatcher("/events.jsp");
				dispatcher.forward(request, response);	     
			}
		}
		else if(sortBased.equalsIgnoreCase("eventsbydate"))
		{
			String start = request.getParameter("fromDate");
			String end = request.getParameter("toDate");
			//check for required field
			if(start.equalsIgnoreCase("") || end.equalsIgnoreCase("") || start==null || end==null )
			{
			System.err.println("Required fields are not entered!");
			request.setAttribute("ServerMessage","Please Enter value in required* fields!");
			dispatcher = getServletContext().getRequestDispatcher("/events.jsp");
			dispatcher.forward(request, response);
			}
		
			//check for date validation
			DateFormat formatter ; 
			formatter = new SimpleDateFormat("MM-DD-yyyy");
			Date date1 = null; 
			Date date2 = null;

			try {
				date1 = (Date)formatter.parse(start);
				date2 = (Date)formatter.parse(end);
			} catch (ParseException e) {
				System.err.println("Exception occured in EventsDAO layer: " + e.getMessage());
				request.setAttribute("ServerMessage","Service Unavailable!");
				dispatcher = getServletContext().getRequestDispatcher("/events.jsp");
				dispatcher.forward(request, response);
			}
			
			Calendar first = Calendar.getInstance();
			Calendar second = Calendar.getInstance();
			first.setTime(date1);
			second.setTime(date2);
			
			if(second.before(first))
			{
				System.out.println("Date is not valid!");
				request.setAttribute("ServerMessage","From Date can not be greater than End Date. Please Enter Valid Date!");
				dispatcher = getServletContext().getRequestDispatcher("/events.jsp");
				dispatcher.forward(request, response);
			}
			try 
			{
				eventsArray = client.getEventsByDate(start, end);
			} catch (GetEventsByDateFaultException e) {
				GetEventsByDateFault fault = e.getFaultMessage();
				String exceptionString = fault.getGetEventsByDateFault();
				
				request.setAttribute("ServerMessage",exceptionString);
				dispatcher = getServletContext().getRequestDispatcher("/events.jsp");
				dispatcher.forward(request, response);	 
			}
		}
		
		EventsFacade[] events =null;
		if(eventsArray!=null)
			{
			events = eventsArray.getEventList();
			}
		
		session.setAttribute("EventsArrayData", events);
		dispatcher = getServletContext().getRequestDispatcher("/events.jsp");
		dispatcher.forward(request, response);	      
	}
	
}
